package org.basic.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.basic.common.core.exception.ServiceException;
import org.basic.sms.domain.SmsParam;
import org.basic.sms.domain.SysSmsLog;
import org.basic.sms.service.MessageService;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

@Slf4j
public class AliyunMessageServiceImpl implements MessageService {




    @Override
    public SysSmsLog sendMessage(SmsParam param, Map<String,String> params) {

        validateParam(param);

        //初始化acsClient，<accessKeyId>和<accessSecret>"在短信控制台查询即可
        DefaultProfile profile = DefaultProfile.getProfile(param.getRegion(), param.getSecretId(), param.getSecretKey());
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        //域名，请勿修改
        request.setSysDomain("dysmsapi.aliyuncs.com");
        //API版本号，请勿修改
        request.setSysVersion("2017-05-25");
        //API名称
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", param.getRegion());
        request.putQueryParameter("PhoneNumbers", param.getPhone());
        request.putQueryParameter("SignName", param.getSignature());
        request.putQueryParameter("TemplateCode", param.getTemplateCode());
        Map<String,String> paramsMap = new LinkedHashMap<>();
        Set<String> stringSet = params.keySet();
        for(String str : stringSet){
            String value = params.get(str);
            paramsMap.put(str, value);

        }
        Gson gson = new Gson();
        String json = gson.toJson(paramsMap);

        request.putQueryParameter("TemplateParam", json);
        try {
            CommonResponse response = client.getCommonResponse(request);
            int httpStatus = response.getHttpStatus();
            SysSmsLog sysSmsLog = new SysSmsLog();
            sysSmsLog.setPhone(sysSmsLog.getPhone());
            sysSmsLog.setRequestLog(response.getData());
            if(httpStatus == 200){
                log.info("发送成功：{}",response.getData());
                sysSmsLog.setSendStatus(0);
            }else{
                log.error("发送失败：{}",response.getData());
                sysSmsLog.setSendStatus(1);
            }
            return sysSmsLog;
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void validateParam(SmsParam param) {
        String region = param.getRegion();
        String signature = param.getSignature();
        String secretId = param.getSecretId();
        String secretKey = param.getSecretKey();
        String templateId = param.getTemplateCode();
        String phone = param.getPhone();
        if(StringUtil.isBlank(region)){
            throw new ServiceException("地区不可为空");
        }
        if(StringUtil.isBlank(signature)){
            throw new ServiceException("短信签名不为空");
        }
        if(StringUtil.isBlank(secretId)){
            throw new ServiceException("密钥ID不为空");
        }
        if(StringUtil.isBlank(secretKey)){
            throw new ServiceException("密钥Key不为空");
        }
        if(StringUtil.isBlank(templateId)){
            throw new ServiceException("模板Code不为空");
        }
        if(StringUtil.isBlank(phone)){
            throw new ServiceException("手机号不为空");
        }

    }
}
